importProjectContext

set packageDir [file join $projectRoot "[file tail $projectRoot]$::tank::version"]
set packageFile [file join $packageDir [file tail [file rootname $::tank::main]].tm]

if [file isdirectory $packageDir] {
    file delete -force $packageDir
}
file copy -force [file join $projectRoot src] $packageDir
file delete [file join $packageDir [file tail $::tank::main]]

set text "# DEPENDEBCIES\n"
append text "lappend auto_path [file join $::tank::path lib]\n"
foreach p $::tank::dependencies {
    append text "package require $p\n"
}
append text "\n# PACKAGE\npackage provide [file tail $projectRoot] $::tank::version\n\n"
withOpen fh [open $::tank::main r] {
    foreach l [split [read $fh] "\n"] {
        if [regexp {\#\s*RUN} $l] {break}
        append text "$l\n"
    }
}
spit $packageFile $text

pkg_mkIndex -verbose $packageDir *.tm
